<?php
namespace app\admin\model;

use think\Model;

class Order extends Model{
    /**
     * 获取所有捐赠记录
     * @author 巫玉梅 <1120504614@qq.com>
     * @return
     */
    public function getAll(){
        $order="";
        $keywords = trim(input('get.keywords'));
        if(!empty($keywords)){
            $where = ['u.full_name|u.tel|u.username' => ['like', "%{$keywords}%"]];
        }else{
            $where = [];
        }

        $data = $this
            ->alias('o')
            ->join('user u', 'o.user_id=u.id', 'LEFT')
            ->join('lesson bld', 'o.lesson_id=bld.id', 'LEFT')
            ->field('o.id,u.full_name,u.tel,o.lesson_id,bld.lesson_sn,
                    bld.lesson_cost,bld.cost_type,bld.lesson_name,
                    o.order_time,o.order_sn,bld.id lid,u.id uid')
            ->where($where)
            ->order($order)
            ->select();
        if($data){
            foreach($data as $key => $value){
                $data[$key] = $value->toArray();
            }
        }

        return $data;
    }

    /**
     * 获取记录数
     * @author 巫玉梅 <1120504614@qq.com>
     * @return
     */
    public function getCount(){
        $order="";
        $keywords = trim(input('get.keywords'));
        if(!empty($keywords)){
            $where = ['u.full_name|u.tel|u.username' => ['like', "%{$keywords}%"]];
        }else{
            $where = [];
        }
        $count = $this
            ->alias('o')
            ->join('user u', 'o.user_id=u.id', 'LEFT')
            ->join('lesson bld', 'o.lesson_id=bld.id', 'LEFT')
            ->field('o.id,u.full_name,u.tel,o.lesson_id,bld.lesson_sn,
                    bld.lesson_cost,bld.cost_type,bld.lesson_name,
                    o.order_time,o.order_sn,bld.id lid,u.id uid')
            ->where($where)
            ->order($order)
            ->count();
        return $count;
    }

    /**
     * 获取订单的详情
     * @author 巫玉梅 <1120504614@qq.com>
     * @return
     */
    public function getOne($where=""){
        $info = $this
            ->alias('o')
            ->join('user u', 'o.user_id=u.id', 'LEFT')
            ->join('lesson bld', 'o.lesson_id=bld.id', 'LEFT')
            ->field('o.id,u.full_name,u.tel,o.lesson_id,bld.lesson_sn,
                    bld.lesson_cost,bld.cost_type,bld.lesson_name,
                    o.order_time,o.order_sn,bld.id lid,u.id uid')
            ->where($where)
            ->find()
            ->toArray();
        return $info;
    }

    /**
     * 获取订单的详情
     * @author 巫玉梅 <1120504614@qq.com>
     * @return
     */
    public function getOrderById($id){

        $info = $this
            ->alias('o')
            ->join('user u', 'o.user_id=u.id', 'LEFT')
            ->join('lesson bld', 'o.lesson_id=bld.id', 'LEFT')
            ->field('o.*,u.full_name as student_name,
                    u.tel,bld.lesson_cost,bld.cost_type,bld.lesson_name,
                    bld.id lid,u.id uid')
            ->where("o.id=".$id)
            ->find()
            ->toArray();

        return $info;
    }
}